package com.shopping.mall.main.config;


import com.shopping.mall.auth.filter.CORSInterceptor;
import com.shopping.mall.auth.filter.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private CORSInterceptor corsInterceptor;
    @Autowired
    private LoginInterceptor loginInterceptor;

    String[] loginPaths = new String[]{
            "/**"
    };

    String[] unLoginPaths = new String[]{
            "/error",
            "/static/**",
            "/webjars/**",
            "/swagger-resources/**",
            "/v2/api-docs",
            "/swagger-ui.html",
            "/test/**",
            "/language/**",
            "/type/**"
    };

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(corsInterceptor).addPathPatterns("/**");
        registry.addInterceptor(loginInterceptor).addPathPatterns(loginPaths).excludePathPatterns(unLoginPaths);
    }
}
